﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Xml;
using BLL = KC.Business.BlogSlinger;
using KC.DataAccess.BlogSlinger;

public partial class sitemap : System.Web.UI.Page
{
	private string cs = System.Configuration.ConfigurationManager.ConnectionStrings["SQL"].ConnectionString;

	protected void Page_Load(object sender, EventArgs e)
	{
		BlogSlingerDataContext dc = new BlogSlingerDataContext(cs);
		int BlogID = -1;
		if (Request.QueryString["ID"] != null && !string.IsNullOrEmpty(Request.QueryString["ID"]))
			int.TryParse(Request.QueryString["ID"], out BlogID);
		Blog blog = BlogID == -1 ? null : BLL.GetBlog(ref dc, BlogID);


		// ID null: Sitemap Index
		// ID = -1: Master Sitemap
		// ID = ##: Sitemap for Blog ##
		XmlDocument xDoc = Request.QueryString["ID"] == null ? BLL.BuildSitemapIndex(ref dc)
			: BlogID == -1 ? BLL.BuildSitemapXML(ref dc)
			: BLL.BuildSitemapXML(ref dc, BlogID);
		
		if (xDoc == null) return;


		Response.Clear();
		Response.ContentType = "application/rss+xml";
		Response.HeaderEncoding = System.Text.Encoding.UTF8;
		Response.Write(BLL.FormatXml(xDoc).Replace("\"utf-16\"", "\"UTF-8\"").Replace(" xmlns=\"\"", ""));
		Response.Flush();
		Response.End();
	}
}